package com.controller;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.po.Admin;
import com.po.EquityRecord;
import com.po.Staff;
import com.service.EquityRecordService;
import com.service.StaffService;

@Controller
//@RequestMapping(value = "/staff")
public class StaffController {
	@Autowired
	private StaffService staffService;
	@Autowired
	private EquityRecordService equityRecordService;
	
	
	@RequestMapping(value = "/staff/list",method = RequestMethod.GET)
	public String list(Model model,@ModelAttribute Staff staff){
		
		
		
		if(staff.getUnamesearch()==null && staff.getDepartmentsearch() ==null)
		{
			 List<Staff> staffs=staffService.list();
			 model.addAttribute("list", staffs);
			   
		}else
		{
			try {
				if (staff != null){
					String a = new String(staff.getUnamesearch().getBytes("ISO8859_1"),"UTF-8");
					String b = new String(staff.getDepartmentsearch().getBytes("ISO8859_1"),"UTF-8");
					staff.setUnamesearch(a);
					staff.setDepartmentsearch(b);
					List<Staff> staffs=staffService.searchlist(staff);
					 model.addAttribute("list", staffs);
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		}

	    return "staff/list";
	}
	
	
	@RequestMapping(value = "/staff/edit")
	public String save(@RequestParam(value="id") Integer id,Model model){
		if(id!=0){
			Staff staff = staffService.findById(id);
			 model.addAttribute("staff", staff);
			 return "staff/detail";
			}
		Staff ad = new Staff ();
		ad.setId(0);
		 model.addAttribute("staff", ad);
	    return "staff/detail";
	}
	
	
	@RequestMapping(value = "/staff/save",method = RequestMethod.POST)
	public String save(@ModelAttribute Staff staff){
		if(staff.getId() != 0){
			staffService.update(staff);
		}else{
			staffService.add(staff);
		}
	    
	    return "redirect:/staff/list.action";
	}
	
	
	@RequestMapping(value = "/staff/addequity",method = RequestMethod.POST)
	public String addequity(@ModelAttribute Staff sta){
		
		Staff staff = staffService.findById(sta.getId());
		staff.setEquityval((staff.getEquityval()+sta.getEquityval()));
		EquityRecord  eq = new EquityRecord();
		eq.setStaffid(sta.getId());
		eq.setNum(sta.getEquityval());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		   try {
			   Date date=sdf.parse(sta.getCreateTime());
			   eq.setCreateTime(date);
			  } catch (ParseException e) {
			   e.printStackTrace();
			  }
		equityRecordService.add(eq); 
			staffService.update(staff);
	    return "redirect:/staff/list.action";
	}
	
	@RequestMapping(value = "/staff/del")
	public String del(@RequestParam(value="id") Integer id){
	    staffService.del(id);
	    return "redirect:/staff/list.action";
	}
	
	
	

}
